package com.eatme.core.sort;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 选择排序
 */
public class SelectionSort {
    private static final Logger LOG = LoggerFactory.getLogger(SelectionSort.class);

    private SelectionSort() {

    }

    public static <E extends Comparable<E>> void sort(E[] arr) {
        for (int i = 0; i < arr.length; i++) {
            // 选择arr[i...n) 中最小值的索引
            int minDataIndex =i;
            for (int j = i; j < arr.length; j++) {
                if (arr[j].compareTo(arr[minDataIndex])<0){
                    minDataIndex =j;
                }
            }
            swap(arr,i,minDataIndex);

        }
    }

    private static <E>void swap(E[] arr, int i, int minDataIndex) {
        E temp = arr[i];
        arr[i] = arr[minDataIndex];
        arr[minDataIndex] =temp;
    }
    public static void main(String[] args) {


    }
}
